<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pejabat extends CI_Controller {
	
        public function __construct()
	{
		parent::__construct();
                $this->load->model('pejabat_model','mb');
                if(!$this->session->userdata('logged_in'))
                {
                  redirect('login');
                }
	}
	
	function pejabatsList($id="")
	{
		if($id != "")
		{
			$selectedPejabat = $this->mb->get($id);
		}
		else
		{
			$selectedPejabat = NULL;
		}
		$data['selectedPejabat'] = $selectedPejabat;
		$data['username'] = $this->session->userdata('logged_in');
		$data['level'] = $this->session->userdata('level');
		$data['pejabats'] = $this->mb->get();
		$this->load->view('pejabats/index', $data);
	}
	
	function savePejabat()
	{
		$this->form_validation->set_rules('kode','Kode','required');
		$this->form_validation->set_rules('kodept','Kode PT','required');
		$this->form_validation->set_rules('uraian','Uraian','required');
		$this->form_validation->set_rules('nama','Nama','required');
		$this->form_validation->set_rules('periode_awal','Periode Awal','required');
		$this->form_validation->set_rules('periode_akhir','Periode Akhir','required');
		
                $this->form_validation->set_message('required','Kolom %s harus diisi dengan benar');
                
                
                if ($this->form_validation->run())
                {
			$id =$this->input->post('kode');
			$pejabatData = array(
				'kode' => $this->input->post('kode'),
				'kodept' => $this->input->post('kodept'),
				'uraian' => $this->input->post('uraian'),
				'nama' => $this->input->post('nama'),
				'periode_awal' => $this->input->post('periode_awal'),
				'periode_akhir' => $this->input->post('periode_akhir'),
				'keterangan' => $this->input->post('keterangan')
				);
			if($this->mb->is_pejabat_exist($id) == true)
			{
				//Update
				$this->mb->update($id, $pejabatData);
				$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$pejabatData['pejabat'].'</b> berhasil di ubah!</p>');
				redirect('pejabat/pejabatsList');
			}
			else
			{
				//Add
				$this->mb->insert($pejabatData);
				$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$pejabatData['pejabat'].'</b> berhasil di tambah!</p>');
				redirect('pejabat/pejabatsList');
			}
                }
                else
                {
                        $this->session->set_flashdata('msg', '<p class="alert alert-error"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a>Isi form dengan benar!</p>');
			redirect('pejabat/pejabatsList');
                }        
	}
	
	function deletePejabat($kodepier,$id="")
	{
		if($this->mb->is_pejabat_exist($id) == true)
		{
			$this->mb->delete($id);
			$this->session->set_flashdata('msg', '<p class="alert alert-success"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>'.$kodepier.'</b> berhasil di hapus!</p>');
			redirect('pejabat/pejabatsList');
		}
		else
		{
			$this->session->set_flashdata('msg', '<p class="alert alert-danger"><a class="close" data-dismiss="alert"><i class="icon-remove"></i></a><b>Tidak bisa hapus diri sendiri!</b></p>');
			redirect('pejabat/pejabatsList');
		}
	}
}